import { Controller, Get, Query, Res, Header } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiQuery } from '@nestjs/swagger';
import { OperationLogService } from './operation-log.service';
// import { JwtAuthGuard } from '../auth/jwt-auth.guard';
// import { RolesGuard } from '../auth/roles.guard';
// import { Roles } from '../auth/roles.decorator';
import { Response } from 'express';

@ApiTags('操作日志')
@Controller('operation-logs')
// @UseGuards(JwtAuthGuard, RolesGuard)
export class OperationLogController {
  constructor(private readonly operationLogService: OperationLogService) {}

  @Get('list')
  @ApiOperation({ summary: '获取操作日志列表' })
  @ApiQuery({ name: 'userId', required: false, type: Number })
  @ApiQuery({ name: 'username', required: false })
  @ApiQuery({ name: 'operationType', required: false })
  @ApiQuery({ name: 'startDate', required: false })
  @ApiQuery({ name: 'endDate', required: false })
  @ApiQuery({ name: 'method', required: false })
  @ApiQuery({ name: 'status', required: false, type: Number })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  async findWithFilters(
    @Query() filters: {
      userId?: number;
      username?: string;
      operationType?: string;
      startDate?: string;
      endDate?: string;
      method?: string;
      status?: number;
      page?: number;
      limit?: number;
    },
  ) {
    return this.operationLogService.findWithFilters({
      ...filters,
      startDate: filters.startDate ? new Date(filters.startDate) : undefined,
      endDate: filters.endDate ? new Date(filters.endDate) : undefined,
    });
  }

  @Get('statistics')
  // @Roles('admin', 'audit')
  @ApiOperation({ summary: '获取操作日志统计信息' })
  async getStatistics(
    @Query('startDate') startDate: string,
    @Query('endDate') endDate: string,
  ) {
    return this.operationLogService.getStatistics(
      new Date(startDate),
      new Date(endDate),
    );
  }

  @Get('export')
  // @Roles('admin', 'audit')
  @ApiOperation({ summary: '导出操作日志' })
  @Header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
  @Header('Content-Disposition', 'attachment; filename=operation-logs.xlsx')
  async exportLogs(@Query() filters: any, @Res() res: Response) {
    const buffer = await this.operationLogService.exportLogs(filters);
    res.send(buffer);
    
  }
}


